﻿/*************************************************************************************
     * CLR 版本：       4.0.30319.42000
     * 类 名 称：       CsINI
     * 机器名称：       KAIKO
     * 命名空间：       PunchCard.Service
     * 文 件 名：       CsINI
     * 创建时间：       2020/12/16 8:56:52
     * 作    者：       WongJunLiang
     * 说    明：
     * 修改时间：
     * 修 改 人：
*************************************************************************************/


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace PunchCard.Service
{
    class CsINI
    {
        #region 链接库kernel32.Dll

        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        #endregion

        #region ini默认写出函数

        public static void WriteIniValue(string strIniName, string section, string key, string value)
        {
            string strInipath = (Directory.GetCurrentDirectory() + "\\" + strIniName);
            WritePrivateProfileString(section, key, value, strInipath);
        }

        #endregion

        #region ini默认读取函数

        public static string ReadRealValue(string strIniName, string section, string key)
        {
            string strInipath = (Directory.GetCurrentDirectory() + "\\" + strIniName);
            StringBuilder User = new StringBuilder(512);
            GetPrivateProfileString(section, key, "", User, 512, strInipath);
            return User.ToString();
        }

        #endregion

        #region 真实读取，读取并写出默认值

        public static string ReadRealValue(string strIniName, string section, string key, string strDefault)
        {
            string strInipath = (Directory.GetCurrentDirectory() + "\\" + strIniName);
            StringBuilder User = new StringBuilder(512);
            GetPrivateProfileString(section, key, "", User, 512, strInipath);
            if (User.ToString() == "")
            {
                WritePrivateProfileString(section, key, strDefault, strInipath);
                GetPrivateProfileString(section, key, "", User, 512, strInipath);
            }
            return User.ToString();
        }

        #endregion
    }

}
